home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Sample Apps / PSample / Sources / SampleLibrary.inc.p < prev    next >
Encoding:
Text File  |  1996-11-19  |  6.5 KB  |  237 lines  |  [TEXT/MPS ]

  1. {
  2.     File:        SampleLibrary.inc.p
  3.  
  4.     Contains:    Implementation of the PSampleLibrary shared library.
  5.  
  6.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. }
  9.  
  10. VAR
  11.     gLightState            : BOOLEAN;        { state of the traffic light }
  12.     gWindow                : WINDOWPTR;    { global pointer to our traffic window }
  13.     gStopRect            : Rect;            { rectangle for stop light, set up by NewTrafficLight }
  14.     gGoRect                : Rect;            { rectangle for go light, set up by NewTrafficLight }
  15.  
  16. (*————————————————————————————————————————————————————————————————————————————————————
  17.     GoGetRect
  18.     
  19.     loads the global rectrangles that are used for drawing the traffic lights.
  20. ————————————————————————————————————————————————————————————————————————————————————*)
  21.     
  22. FUNCTION GoGetRect(rectID: INTEGER; VAR theRect: Rect) : BOOLEAN;
  23.  
  24. TYPE
  25.     RectPtr        = ^Rect;
  26.     RectHnd        = ^RectPtr;
  27. VAR
  28.     resource    : Handle;
  29.  
  30. BEGIN
  31.     resource := GetResource('RECT', rectID);
  32.     IF resource <> NIL THEN BEGIN
  33.         GoGetRect := TRUE;
  34.         theRect := RectHnd(resource)^^;
  35.     END
  36.     ELSE
  37.         GoGetRect := FALSE;
  38. END; {GoGetRect}
  39.  
  40. (*————————————————————————————————————————————————————————————————————————————————————
  41.     NewTrafficLight
  42.     
  43.     allocate the traffic window and initilize the traffic light globals.
  44. ————————————————————————————————————————————————————————————————————————————————————*)
  45.  
  46. FUNCTION NewTrafficLight : OSErr;
  47. VAR
  48.     savedrefnum        : LONGINT;
  49.     err                : OSErr;
  50.     themenu            : MenuHandle;
  51.     libraryfile        : TLibraryFilePtr;
  52.     gotrect            : BOOLEAN;
  53.  
  54. BEGIN        
  55.     gLightState := false;
  56.  
  57.     { include the library's file in resource chain so we can allocate our menu
  58.        and window resources }
  59.     
  60.     libraryfile := GetLocalLibraryFile;
  61.  
  62.     err := Preflight( libraryfile, savedrefnum );
  63.  
  64.     IF ( err = kNoError ) THEN BEGIN
  65.  
  66.         gWindow := GetNewWindow( rWindow, NIL, WindowPtr(-1) );
  67.  
  68.         gotrect := GoGetRect(rStopRect, gStopRect);{ get rectangle for Stop light }
  69.         gotrect := GoGetRect(rGoRect, gGoRect);    { get rectangle for Go light }
  70.  
  71.         { if there is a MENU resources in our shared library we should append it 
  72.             to our application }
  73.  
  74.         themenu := GetMenu( mLight );    { get our traffic light menu }
  75.         InsertMenu( themenu, 0 );            { append to the application's menu }
  76.     
  77.         DrawMenuBar;                    { go ahead and update the menu bar }
  78.     
  79.         { Now we are done with accessing our library resource; restore the chain
  80.           to it previous state }
  81.     
  82.         err := Postflight( libraryfile, savedrefnum );
  83.  
  84.     END;
  85.     
  86.     NewTrafficLight := err;
  87.  
  88. END;
  89.  
  90. (*————————————————————————————————————————————————————————————————————————————————————
  91.     FreeTrafficLight
  92.     
  93.     we are done, so dispose the windowrecord
  94. ————————————————————————————————————————————————————————————————————————————————————*)
  95.  
  96. PROCEDURE FreeTrafficLight;
  97. BEGIN
  98.     DisposeWindow( gWindow );            { we are done}
  99. END;
  100.  
  101. (*————————————————————————————————————————————————————————————————————————————————————
  102.     GetLightState
  103.     
  104.     return current state of traffic light
  105. ————————————————————————————————————————————————————————————————————————————————————*)
  106.  
  107. FUNCTION GetLightState : BOOLEAN;
  108. BEGIN
  109.     GetLightState := gLightState;            { return the state of Traffic Light }
  110. END;
  111.  
  112. (*————————————————————————————————————————————————————————————————————————————————————
  113.     SetLightState
  114.     
  115.     set the light to the new state
  116. ————————————————————————————————————————————————————————————————————————————————————*)
  117.  
  118. PROCEDURE SetLightState( newState : BOOLEAN );
  119. BEGIN
  120.     gLightState := newState;            { set the new state }
  121.     
  122.     SetPort(gWindow);                    { make sure we are at the right port before }
  123.     InvalRect( gWindow^.portRect);        { invalidate update region }
  124. END;
  125.  
  126. (*————————————————————————————————————————————————————————————————————————————————————
  127.     DrawLight
  128.     
  129.     draw the actual traffic light
  130. ————————————————————————————————————————————————————————————————————————————————————*)
  131.  
  132. PROCEDURE DrawLight;
  133. VAR
  134.     oldport        : GrafPtr;
  135. BEGIN
  136.     
  137.     IF( gWindow <> NIL ) THEN BEGIN
  138.     
  139.         GetPort( oldport );                { save the old port }
  140.         SetPort( gWindow );                { set the new port to our object's port }
  141.                 
  142.         EraseRect(gWindow^.portRect);    { clear out any garbage that may linger }
  143.         IF ( gLightState ) THEN            { draw a red (or white) stop light }
  144.             ForeColor(redColor)
  145.         ELSE
  146.             ForeColor(whiteColor);
  147.         
  148.         PenSize( 2, 2 );                
  149.         PaintOval(gStopRect);            
  150.         ForeColor(blackColor);
  151.         FrameOval(gStopRect);
  152.         IF gLightState = FALSE THEN        { draw a green (or white) go light }
  153.             ForeColor(greenColor)
  154.         ELSE
  155.             ForeColor(whiteColor);
  156.         PaintOval(gGoRect);
  157.         ForeColor(blackColor);
  158.         FrameOval(gGoRect);
  159.         PenSize( 1, 1 );
  160.  
  161.         SetPort( oldport );                { restore the grafpointer }
  162.     END;
  163. END;
  164.  
  165. (*————————————————————————————————————————————————————————————————————————————————————
  166.     AdjustTrafficLightMenus
  167.     
  168.     Enable or disable traffic light's menus based on the current state.
  169. ————————————————————————————————————————————————————————————————————————————————————*)
  170.  
  171. PROCEDURE AdjustTrafficLightMenus( active: BOOLEAN );
  172.  
  173. VAR
  174.     menu            : MenuHandle;
  175.     savedrefnum        : LONGINT;
  176.     libraryfile        : TLibraryFilePtr;
  177.     err                : OSErr;
  178.  
  179. BEGIN    
  180.     { include the library in resource chain so we can allocate our menu resources }
  181.  
  182.     libraryfile := GetLocalLibraryFile;
  183.     err := Preflight( libraryfile, savedrefnum );
  184.  
  185.     IF( err = noErr ) THEN BEGIN 
  186.         menu := GetMenuHandle( mLight );
  187.         IF(  menu <> NIL ) THEN BEGIN        { get the handle to traffic light menu }
  188.             
  189.             IF ( active ) THEN BEGIN        { do we need to enable them? }
  190.                 EnableItem(menu, iStop);
  191.                 EnableItem(menu, iGo);
  192.             END
  193.             ELSE BEGIN
  194.                 DisableItem(menu, iStop);
  195.                 DisableItem(menu, iGo);
  196.             END;
  197.     
  198.             CheckItem(menu, iStop, gLightState); { we can also determine check/uncheck state, too }
  199.             CheckItem(menu, iGo, NOT gLightState);        
  200.         END;
  201.     
  202.         { restore the resource chain back to it previous state before preflight }
  203.         err := Postflight( libraryfile, savedrefnum );
  204.     END;
  205.  
  206. END;
  207.  
  208. (*————————————————————————————————————————————————————————————————————————————————————
  209.     DoTrafficLightMenuCommand
  210.     
  211.     Enable and disable menus based on the current state of traffic light.
  212. ————————————————————————————————————————————————————————————————————————————————————*)
  213.  
  214. PROCEDURE DoTrafficLightMenuCommand( menuItem: INTEGER );
  215.  
  216. VAR
  217.     savedrefnum        : LONGINT;
  218.     libraryfile        : TLibraryFilePtr;
  219.     err                : OSErr;
  220.  
  221. BEGIN
  222.  
  223.     { include the library in resource chain so we can allocate our menu resources }
  224.  
  225.     libraryfile := GetLocalLibraryFile;
  226.     err := Preflight( libraryfile, savedrefnum );
  227.  
  228.     CASE menuItem OF
  229.         iStop:
  230.             SetLightState( TRUE );                { set new state and update }
  231.         iGo:
  232.             SetLightState( FALSE );                { set new staet and update }
  233.     END;
  234.     { restore the resource chain back to it previous state before preflight }
  235.     err := Postflight( libraryfile, savedrefnum );
  236. END;
  237.